﻿using DFGK.IService;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Models;
using DFGK_WMS.Model.Models.LyModel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace DFGK_WMS.WebAPI.Controllers.PC.装备维修管理
{
    /// <summary>
    /// 报废工单
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PC")]
    public class ScrappedWorkController : BaseController
    {
        private readonly IScrappedWorkService _scrappedWorkService;
        public ScrappedWorkController(IWebHostEnvironment environment, IScrappedWorkService scrappedWorkService) : base(environment)
        {
            _scrappedWorkService = scrappedWorkService;
        }
        /// <summary>
        /// 报废工单添加或修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddOrModefiyScrappedWork(ScrappedWork model)
        {
            // 获取当前用户信息和当前时间，避免多次调用
            var currentUserName = base.BaseGetNowUserName();
            var currentTime = DateTime.Now;
            if (model.Id > 0)
            {
                model.ModifyDate = currentTime;
                model.ModifyUserId = base.BaseGetNowUserId();
                model.ModifyUserName = base.BaseGetNowUserName();
                if (model.Operation.HasValue)
                {
                    switch (model.Operation)
                    {
                        //申请人
                        case 1:
                            model.Applicant = currentUserName;
                            model.ApplicantDate = currentTime;
                            break;
                        //审批人
                        case 2:
                            model.Approver = currentUserName;
                            model.ApproverDate = currentTime;
                            break;
                        //处理人
                        case 3:
                            model.Handler = currentUserName;
                            model.HandlerDate = currentTime;
                            break;
                    }
                }
                bool result = _scrappedWorkService.AddOrModefiyScrappedWork(model, out string message);
                if (result)
                {
                    return Ok(BaseResult.Result(true, "修改成功！", "200"));
                }
                return Ok(BaseResult.Result(false, "修改失败！" + message, "500"));
            }
            else
            {

                model.CreateUserName = base.BaseGetNowUserName();
                model.CreateUserId = base.BaseGetNowUserId();
                model.CreateDate = DateTime.Now;
                if (model.Operation.HasValue)
                {
                    switch (model.Operation)
                    {
                        //申请人
                        case 1:
                            model.Applicant = currentUserName;
                            model.ApplicantDate = currentTime;
                            break;
                        //审批人
                        case 2:
                            model.Approver = currentUserName;
                            model.ApproverDate = currentTime;
                            break;
                        //处理人
                        case 3:
                            model.Handler = currentUserName;
                            model.HandlerDate = currentTime;
                            break;
                    }
                }
                bool result = _scrappedWorkService.AddOrModefiyScrappedWork(model, out string message);
                if (result)
                {
                    return Ok(BaseResult.Result(true, "添加成功！", "200"));
                }
                return Ok(BaseResult.Result(false, "添加失败！" + message, "500"));


            }
        }
        /// <summary>
        /// 删除报废工单
        /// </summary>
        /// <param name="scrappedWorkId"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelScrappedWork(long scrappedWorkId)
        {
            if (scrappedWorkId <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var result = _scrappedWorkService.DelScrappedWork(scrappedWorkId);
            if (result)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！", "500"));
        }
        /// <summary>
        /// 报废工单id查询
        /// </summary>
        /// <param name="scrappedWorkId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetScrappedWork(long scrappedWorkId)
        {
            if (scrappedWorkId <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var shareInfo = _scrappedWorkService.GetScrappedWork(scrappedWorkId);
            if (shareInfo != null)
            {
                return Ok(BaseResult.Result(shareInfo, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetScrappedWorkData(string? name,  int pageIndex = 1, int pageSize = 15)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                BaseResult.Result("400", "分页条件有误！");
            }
            var list = _scrappedWorkService.GetScrappedWorkData(name, out var totalCount, pageIndex, pageSize);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }
    }
}
